/*
 * 作者: 侯晓宇
 * 时间: 2017-02-17
 * 说明：
 * Copyright (c) 2016-2017 中研沃兮
 */
define(function(require) {
	require("jquery");
    var c = require('app/utils/CommonUtils');
	var hc = require('app/utils/HttpClientUtils');
	var tip = require('app/utils/TipUtils');
	var EventUtils = require('app/utils/EventUtils');
	var win = window;

	var param = api.pageParam;
	var MsgInfoUtils = require('app/utils/MsgInfoUtils');
	var TimerUtils = require('app/utils/TimerUtils');
	var AudioUtils = require('app/utils/AudioUtils');
	var VideoUtils = require('app/utils/VideoUtils');
	var UserInfoUtils = require('app/utils/UserInfoUtils');
	var WeixinUtils = require('app/utils/WeixinUtils');
	var UIChatBox = api.require('UIChatBox');
	var fs = api.require("fs");
	var action = {};
	var currentppt = param.currentppt;

	var temp_record_path = 'fs://record/audio_record_only_this.amr';

	EventUtils.listen(api.winName,'updatePPTstatus',false, function(data){
		currentppt = parseInt(data.index);
	});

	action.close = function(){
		var tab_on = $("#tab_div").val();
		if(tab_on == 1){
			if(AudioUtils.isRecording()){
				AudioUtils.btn_audio_cancel(function(){
					api.closeFrame({
					});
				});
			}else{
				api.closeFrame({
				});
			}

		}else if(tab_on == 2){
			UIChatBox.hide();
	        UIChatBox.close();


			api.closeFrame({
	        });
		}else if(tab_on == 3){
			$("#more_div").hide();
			api.closeFrame({
	        });
		}else {
			api.closeFrame({
	        });
		}



	}

	action.sendMessage = function(msg){
		//data.msg, data.type - txt

		var data = MsgInfoUtils.buildSendMsg(msg);


		if(msg.type == 'audio'){

			var new_local_path = 'fs://record/'+data.msgid+'.amr';

			var ret = fs.renameSync({
			    oldPath: temp_record_path,
			    newPath: new_local_path
			});
			c.debug("rename_temp_record:ret",ret);
			if (ret.status) {
				msg.content = new_local_path;

			} else {
			    api.toast({
                    msg:'语音录制失败，请重试'
                });
                return;
			}

		}else if(msg.type == 'img'){

		}

		//RoomBodyFrm接收此消息，并绘制。roomwin接收，发送
		EventUtils.send(api.winName, 'send_room_msg', data);

		AudioUtils.resetRecord();
	}

	action.launchVideo = function(){

		$("#video_div").show();
		AudioUtils.showtips("提示：点击上方空白处即可关闭");

		$("#smalllive").click(function(){
			VideoUtils.startSmallLive();
		});

		$("#fulllive").click(function(){
			VideoUtils.startFullLive();
		});

		$("#pclive").click(function(){
			VideoUtils.startPcLive();
		});
	}

	action.launchText = function(){

		UIChatBox.open({
            placeholder: '输入发送内容',
            autoFocus: true,
            maxRows: 1,
            emotionPath: 'widget://res/room/emotion',
            styles: {
                indicator: {
                    target: 'extrasPanel',
                    color: '#c4c4c4',
                    activeColor: '#9e9e9e'
                },
                sendBtn: {
                    titleColor: '#FFFFFF',
                    bg: '#999999' ,
                    activeBg: '#46a91e',
                    titleSize: 14
                }
            }
        }, function( ret, err ){

        	if(ret.eventType == "send"){

        		if(ret.msg.length > 300){
        			tip.show("输入不能超过300字");
        			return;
        		}

        		if(c.isNullStr(ret.msg)){
        			tip.show("输入不能为空");
        			return;
        		}

        		action.sendMessage({content:ret.msg, type:"txt"});
        		action.close();
        	}
        });
	}


	//////////////////////////////////////////////////////////////////////////////////////////////

	action.launchAudio = function(){
		AudioUtils.showtips("录音会与上面幻灯片保持对应");
		$("#audio_div").show();
		$("#btn_audio_record").click(function(){
			AudioUtils.startRecord(function(msg){//60s自动停止
			//发送语音至服务器
				hc.uploadAudio(temp_record_path, null, 'audio', function(resp){

					var newsrc = resp.result.path;
					msg.newsrc = newsrc;
					msg.ppt = currentppt+"";

					c.debug("newsrc audio: ",msg);
					action.sendMessage(msg);

				});
			});
		});
		$("#btn_audio_stop").click(function(){
			AudioUtils.stopRecord();
		});
		$("#btn_audio_send").click(function(){
			AudioUtils.sendRecord(function(msg){
			//		var msg = {
			//  		sec: opt.c_audio.sec,
			//  		type: 'audio'
			//  	};
				//发送语音至服务器
				hc.uploadAudio(temp_record_path, null, 'audio', function(resp){

					var newsrc = resp.result.path;
					msg.newsrc = newsrc;
					msg.ppt = currentppt+"";

					c.debug("newsrc audio: ",msg);
					action.sendMessage(msg);

				});
			});
		});
		$("#btn_audio_preview").click(function(){
			AudioUtils.btn_audio_preview();
		});
		$("#btn_audio_cancel").click(function(){
			AudioUtils.btn_audio_cancel();
		});
	}

	action.launchMore = function(){
		$("#more_div").show();
		AudioUtils.showtips("提示：点击上方空白处即可关闭");

		if(UserInfoUtils.getLoginedUser().userid == param.creatoruuid){
			if(param.liveType == "2"){
				$("#ppt").show();
			}

			$("#setting").show();
		}


		$("#pcstreamurl").click(function(){
			VideoUtils.startPcLive();
		});

		$("#photo").click(function(){
			var _sourceType = "library";
			api.getPicture({
			    sourceType: _sourceType, // camera,library
			    encodingType: 'jpg',
			    mediaValue: 'pic',
			    destinationType: 'url',
			    allowEdit: false,
			    quality: 100,
			    saveToPhotoAlbum: false,
			    targetWidth: 800,
    			targetHeight: 800
			}, function(ret, err) {

				var fileurl = ret.data;
				if(c.isNullStr(fileurl)){
					return;
				}


				hc.uploadImg(fileurl, null ,"chatimg",function(resp){
					if(resp.status == "201"){
						var msg = {};
						msg.content = fileurl;
						msg.newsrc = resp.result.path;
						msg.type = "img";
						action.sendMessage(msg);
						action.close();
					}
				});


			});
		});

		$("#camera").click(function(){

			var _sourceType = "camera";
			api.getPicture({
			    sourceType: _sourceType, // camera,library
			    encodingType: 'jpg',
			    mediaValue: 'pic',
			    destinationType: 'url',
			    allowEdit: false,
			    quality: 100,
			    saveToPhotoAlbum: false,
			    targetWidth: 800,
    			targetHeight: 800
			}, function(ret, err) {

				var fileurl = ret.data;

				if(c.isNullStr(fileurl)){
					return;
				}

				hc.uploadImg(fileurl, null ,"chatimg",function(resp){
					if(resp.status == "201"){
						var msg = {};
						msg.content = fileurl;
						msg.newsrc = resp.result.path;
						msg.type = "img";
						action.sendMessage(msg);
						action.close();
					}
				});


			});



		});

		$("#share").click(function(){


			api.actionSheet({
			    title: '更多操作',
			    cancelTitle: '关闭',
			    buttons: ['分享给微信好友', '分享到微信朋友圈']
			}, function(ret, err) {
			    var index = ret.buttonIndex;
			    if(index == "1"){
			    	WeixinUtils.share(1, param.name, param.uuid);
			    }else if(index == "2"){
			    	WeixinUtils.share(2, param.name, param.uuid);
			    }
			});


//			share: function(share_type, title,roomuuid, calback_func){
		});

		$("#like").click(function(){

		});

		$("#ppt").click(function(){

		});

		$("#laud").click(function(){
			api.openWin({
					name: 'WxpayWin',
					url: './WxpayWin.html',
					pageParam: param
			});

		});

		$("#setting").click(function(){


		});
	}

	return action;
});
